package biblio.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Area;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import biblio.model.Biblio;
import biblio.utils.Search;


/**
 * Classe qui gere l'interface graphique de recherche
 * @author Groupe 2
 * @version 29/10/13
 */
public class SearchWindow extends JFrame implements ActionListener {
	
	/**L'outil de recherche utilise dans le GUI*/
	Search search;
	
	/*
	 * Elements de l'interface graphique
	 */
	private JPanel panel;
	private JPanel searchPanel;
	private JPanel resultPanel;
	
	private JButton searchButton;
	
	private JTextField searchField;
	
	private JLabel title;
	private JLabel result;
	
	private JTextArea textArea;
	
	/**
	 * Cree un GUI de recherche associe a la bibliotheque passee en 
	 * argument
	 */
	public SearchWindow(Biblio biblio){
		// Initialisation des differents composants du GUI
		search = new Search(biblio);
		panel = new JPanel(new BorderLayout());
		searchPanel = new JPanel(new BorderLayout());
		resultPanel = new JPanel(new BorderLayout());
		
		searchField = new JTextField();
		searchButton = new JButton("Search");
		
		// Listener
		searchField.getDocument().addDocumentListener(new DocumentListener() {
			  public void changedUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  public void removeUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  public void insertUpdate(DocumentEvent e) {
				  enableSearchButton();
			  }
			  
			  public void enableSearchButton(){
				  if(searchField.getText().equals(""))
						searchButton.setEnabled(false);
				  else
				    	searchButton.setEnabled(true);
			  }
		});
		
		// Si on n'entre rien comme recherche, on desactive le bouton
		if(searchField.getText().equals(""))
			searchButton.setEnabled(false);
		
		title = new JLabel("Title");
		result = new JLabel("Result");
		
		textArea = new JTextArea(5,30);
		textArea.setEditable(false);
		
		searchButton.addActionListener(this);
		
		searchPanel.add(title, BorderLayout.NORTH);
		searchPanel.add(searchField, BorderLayout.CENTER);
		searchPanel.add(searchButton, BorderLayout.EAST);
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		
		resultPanel.add(result, BorderLayout.NORTH);
		resultPanel.add(scrollPane, BorderLayout.CENTER);
		
		panel.add(searchPanel, BorderLayout.NORTH);
		panel.add(resultPanel, BorderLayout.CENTER);
		
		this.add(panel);
		
		this.setTitle("Library");
		this.setVisible(true);
		this.setSize(400, 150);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pack();
	}

	/**
	 * Listener pour le bouton de recherche
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton b = (JButton) e.getSource();
		
		if(b == searchButton){
			textArea.setText("");
			if(search.searchTitle(searchField.getText()) != null) textArea.setText(search.searchTitle(searchField.getText()).toString());
			else textArea.setText("Title not found or too many occurrences");
		}
	}
}
